package keter.security;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class CommandInjection {
    
    public static void cmdExecution(String ip){
        Runtime rt = Runtime.getRuntime();
        try {
            Process proc =  rt.exec("cmd.exe /C ping " + ip);
            print(proc); 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void print(Process proc) throws UnsupportedEncodingException, IOException {
        InputStream stderr =  proc.getInputStream(); // 获取输入流 
        InputStreamReader isr = new InputStreamReader(stderr,"GBK" );//"UTF-8"
        BufferedReader br = new BufferedReader(isr);  
        String line = null;  
        while ((line = br.readLine()) != null) { // 打印出命令执行的结果  
            System.out.println(line);  
        }
    }
    
    public static void main(String[] args) {
//        cmdExecution("127.0.0.1");
        cmdExecution("127.0.0.1 & ls");
    }
   

}
